//
//  HMNetworkTools.swift
//  sinaweibo
//
//  Created by lst on 16/5/22.
//  Copyright © 2016年 lst. All rights reserved.
//

import UIKit
import AFNetworking
//声明枚举
//枚举任意类型
enum HTTPMethods: Int {
    case POST
    case GET
}

struct API {
    static let hostName = "https://api.weibo.com/"
    //加载用户token
    static let loadAccessToken = "oauth2/access_token"
    static let lodeUserInfo = "2/users/show.json"
    //首页数据加载
    static let loadHomedata = "2/statuses/home_timeline.json"
}


class HMNetworkTools: AFHTTPSessionManager {
    //声明单例对象
    static let sharedTools: HMNetworkTools = {
        //把url前面一样的部分  声明为基础url地址
        let url = NSURL(string: API.hostName)
        let tools = HMNetworkTools(baseURL: url)
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
    }()
    
    
    
    //封装 get & post 请求方法
    
    func request(method: HTTPMethods,urlString: String,parameters: AnyObject?,finished: (responseObject: AnyObject?,error: NSError?) -> ()){
        //定义闭包参数 将相同的闭包当做参数传递
        let success = { (task: NSURLSessionDataTask,result: AnyObject?) -> ()in
            //执行请求成功回调
            
            finished(responseObject: result,error: nil)
        }
        let failure = { (task: NSURLSessionDataTask?, error: NSError) -> () in
            //执行失败的回调
            print(error)
            finished(responseObject: nil, error: error)
        }
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
        
    }
    
}
///MARK: 将用户认证 获取用户信息相关的方法归到这个分类里面
extension HMNetworkTools{
    //请求用户的token
    func loadAccessToken(code: String, finished: (responseObject: AnyObject?, error: NSError?) -> ()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id" : client_id, "client_secret":client_secret, "grant_type": "authorization_code","code":code,"redirect_uri":redirect_uri]
        request(.POST, urlString: urlString, parameters: parameters, finished: finished)
    }
    
    //加载用户信息
    func loadUserInfo(response: [String : AnyObject],finished: (responseObject: AnyObject?, error: NSError?) -> ()) {
        
        //用户访问接口权限的标识
        let token = response["access_token"]!
        let uid = response["uid"]!
        
        let urlStirng = "https://api.weibo.com/2/users/show.json"
        //字典中可以添加 nil嘛
        let parameters = ["access_token": token, "uid": uid]
        request(.GET, urlString: urlStirng, parameters: parameters, finished: finished)
    }
}
//首页相关
extension HMNetworkTools{
    func loadHomePageData(finished: (responseObject: AnyObject?, error: NSError?) -> ()){
        guard let token = HMUserAccountViewModel.sharedAccountViewModel.userAccount?.access_token else{
            return
        }
        let parameters = ["access_token": token,"count": "50"]
        request(.GET, urlString: API.loadHomedata, parameters: parameters, finished: finished)
    }
}

